package org.example.top40;

public class main29 {
    //相交链表
    public static void main(String[] args) {
        ListNode headA = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(5);
        headA.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        ListNode headB = new ListNode(9);
        ListNode nodeB = new ListNode(10);
        headB.next=nodeB;
        nodeB.next=node2;
        //开始相交
        ListNode a=headA;
        ListNode b=headB;
        while (a!=b){
            a=a==null?headB:a.next;
            b=b==null?headA:b.next;
        }
        System.out.println(a.val);

    }
}
